package com.ibeetl.admin.core.service;

import com.ibeetl.admin.core.dao.TodoDao;
import com.ibeetl.admin.core.entity.CoreRole;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.Test;
import com.ibeetl.admin.core.entity.Todo;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.RoleConstant;
import com.ibeetl.admin.core.web.query.TestQuery;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Test Service
 */

@Service
@Transactional
public class TodoService extends BaseService<Test>{

    @Autowired
    CorePlatformService platformService;
    
    @Autowired private TodoDao testDao;

    public PageQuery<Test>queryByCondition(PageQuery query){
        PageQuery ret =  testDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelTest(List<Long> ids){
        try {
            testDao.batchDelTestByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除Test失败", e);
        }
    }

    public List<Todo> getTodoList(TestQuery query) {
        return testDao.queryTodoList(query);
    }

    public List<Todo> getCompanyTodoList(TestQuery query) {
        return testDao.queryCompanyTodoList(query);
    }

    public List<Todo> getCheckTodoList(TestQuery query) {
        return testDao.queryCheckTodoList(query);
    }
    
    public int todoConut(){
        CoreUser user = platformService.getCurrentUser();
        CoreRole role = platformService.getRole();

        List<Todo> result = new ArrayList<>(16);
        if (role.getId() == RoleConstant.PERSON) {
            TestQuery query = new TestQuery();
            query.setUserId(user.getId());
            query.setBusinessStateCode("0");
            result = getTodoList(query);
        }
        /*
        if role is company
         */
        else if (role.getId() == RoleConstant.COMPANY) {
            TestQuery query = new TestQuery();
            query.setUserId(user.getId());
            query.setBusinessStateCode("1");
            result = getCompanyTodoList(query);
        }
        /*
        if role is check
         */
        else if (role.getId() == RoleConstant.FIRSTCHECK) {
            TestQuery query = new TestQuery();
            query.setBusinessStateCode("2");
            result = getCheckTodoList(query);
        } else if (role.getId() == RoleConstant.FINALCHECK) {
            TestQuery query = new TestQuery();
            query.setBusinessStateCode("3");
            result = getCheckTodoList(query);
        }
        int sum = 0;
        for (Todo todo : result){
            sum += todo.getCount();
        }
        return sum;
    }
}